import Axios from "axios"
let axios = Axios.create({
    baseURL: 'http://127.0.0.1:3003/',
})

export async function getList(){
	try{
		const {data}=await axios.get("get")
		console.log("成功:");
		return {
			code:0,
			data,
			msg:"获取商品数据成功"
		}
	}catch(err){
		console.log("失败:");
		return {
			code:500,
			msg:"服务器错误",
			errMsg:err,

		}
	}
}

export function initWs(param={
	id,code
},fun=(message) => {//信息处理
	console.log("收到的信息",message.data)
}){
	const ws = new WebSocket('ws://localhost:3001')
	ws.onmessage = fun
	ws.onopen = function (evt) {//刚刚打开进行数据同步发送code和id
		ws.send("code-"+JSON.stringify({
			code:param.code,
			id:param.id
		}));
	};
	ws.onclose = function (evt) {//ws被关闭
		console.log("关闭");
	};
	ws.id=param.id
	ws.code=param.code
	wsArr.push(ws)
	return ws
}
const wsArr=[]//保存ws,不用纠结为什么用数组 不用也可以

window.onbeforeunload = function () {
	wsArr.forEach(ws=>{
		ws.send("close-"+JSON.stringify({
			id:ws.id,
			code:ws.code
		}));
	})
	return "";
}